<?php 
HeaderScripts::addScripts(array(
	'admin/spinner.js',
	'admin/spinnerField.js',
	'admin/multiSelect.js',
	'admin/itemSelector.js',
	'admin/fileUploadField.js'
));
HeaderScripts::addStyles(array(
	'admin/spinner.css',
	'admin/multiSelect.css',
	'admin/fileUploadField.css',
	'admin/form.css'
));
?>
<div style="display:none;">
<textarea name="excursionTxt" id="excursionTxt">
<?php echo htmlspecialchars($excursion->attributes['route']) ?>
</textarea>
<?php foreach($attractions as $attraction) { ?>
<textarea name="attractionTxt<?php echo $attraction->attributes['id'] ?>" id="attractionTxt<?php echo $attraction->attributes['id'] ?>">
<?php echo htmlspecialchars($attraction->attributes['description']) ?>
</textarea>
<?php } ?>
</div>
<script type="text/javascript">
var excursionTypesArr  = <?php echo CJSON::encode($excursionTypes)?>;

Ext.namespace('Ext.excursions');

<?php $countAttractions = count($attractions); ?>
var attractionFieldCounter = <?php echo ($countAttractions > 0 ? $countAttractions : 1) ?>;
var excursionform = null;
var currentFieldSet = <?php echo intval($excursion->attributes['excursionTypeId']) ?>;

Ext.onReady(function(){

	Ext.excursions.data = excursionTypesArr;
	
    Ext.QuickTips.init();

    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'side';

    excursionform = new Ext.FormPanel({
        labelWidth: 75, // label settings here cascade unless overridden
        standardSubmit: true,
		fileUpload: true,
		url: '/admin/',
        frame:true,
        title: 'Редактировать экскурсию',
        bodyStyle:'padding:5px 5px 0',
        width: 600,

        items: [{
            xtype:'fieldset',
            id: 'generalInformation',
            title: 'Общая информация',
            collapsible: true,
            autoHeight:true,
            defaults: {width: 210},
			labelWidth: 130,
            defaultType: 'textfield',
            items :[new Ext.form.ComboBox({
                    fieldLabel: 'Тип экскурсии',
                    hiddenName: 'Excursion[excursionTypeId]',
                    store: new Ext.data.ArrayStore({
                        fields: ['value', 'excursion'],
                        data : Ext.excursions.data
                    }),
                    displayField: 'excursion',
                    editable: false,
					valueField: 'value',
					value: <?php echo $excursion->attributes['excursionTypeId'] ?>,
                    typeAhead: true,
                    mode: 'local',
                    triggerAction: 'all',
                    emptyText: 'Выберите тип',
                    selectOnFocus:true,
                    width: 200,
					allowBlank:false,
					blankText: 'Поле обязательно для заполнения',
					listeners:{
                		scope: {},
                		'select': changeFieldSet
           			}
       					
			}),{
                fieldLabel: 'Название',
                name: 'Excursion[title]',
                allowBlank:false,
				blankText: 'Поле обязательно для заполнения',
				value: '<?php echo addcslashes($excursion->attributes['title'], "'\\") ?>'
			}]

        },{
            xtype:'fieldset',
            id: 'setType<?php echo $excursionTypes[0][0] ?>',
            title: '<?php echo $excursionTypes[0][1] ?> экскурсия',
            hidden: <?php echo (($excursion->attributes['excursionTypeId'] != $excursionTypes[0][0]) ? 'true' : 'false') ?>,
            autoHeight:true,
            defaults: {width: 210},
			labelWidth: 130,
            defaultType: 'textfield',
            items :[{
                name: 'Excursion[route<?php echo $excursionTypes[0][0] ?>]',
                xtype: 'ckeditor',
                hideLabel: true,
                CKConfig: {
                    customConfig : '<?php echo Yii::app()->request->baseUrl; ?>/public/assets/ckeditor/config.js',
                    toolbar: 'Str',
                    height : 300,
                    width: 540
                },
				width: 420,
				height: 70,
				value: <?php echo (($excursion->attributes['excursionTypeId'] == $excursionTypes[0][0]) ? 'document.getElementById(\'excursionTxt\').value' : "''") ?>
			},{
				xtype: 'spinnerfield',
				fieldLabel: 'Цена (руб)',
				name: 'Excursion[price<?php echo $excursionTypes[0][0] ?>]',
				minText: 'Минимальное допустимое значение: {0}',
				minValue: 0,
				allowDecimals: true,
				decimalPrecision: 2,
				incrementValue: 1000,
				accelerate: true,
				value: <?php echo (($excursion->attributes['excursionTypeId'] == $excursionTypes[0][0]) ? $excursion->attributes['price'] : 0) ?>
        	},{
				xtype: 'spinnerfield',
				fieldLabel: 'Цена (шекель)',
				name: 'Excursion[price<?php echo $excursionTypes[0][0] ?>Shek]',
				minText: 'Минимальное допустимое значение: {0}',
				minValue: 0,
				allowDecimals: true,
				decimalPrecision: 2,
				incrementValue: 1000,
				accelerate: true,
				value: <?php echo (($excursion->attributes['excursionTypeId'] == $excursionTypes[0][0]) ? $excursion->attributes['price2'] : 0) ?>
        	},{
        		xtype:'fieldset',
				id: 'attractions',
				itemId: 'attractions',
				width: 550,
				autoHeight:true,
				title: 'Достопримечательности',
				items: [<?php if ($countAttractions == 0) { ?>{
					id: 'attraction0',
					xtype:'fieldset',
					width: 500,
					labelWidth: 110,
					autoHeight:true,
					style: 'border: 0px; margin-bottom: 0px;',
					items: [{
						xtype:'textfield',
						fieldLabel: 'Название',
	    				name: 'Attraction[title][]',
	    				id: 'attractionTitle0',
	    				width: 300
					},{
						fieldLabel: 'Описание',
						xtype:'textarea',
	        			name: 'Attraction[description][]',
	        			id: 'attractionDescription0',
	        			width: 300
					},{
						xtype: 'button',
						text: 'удалить',
						style: 'padding-left: 115px',
						handler: function() {
							excursionform.findById('attractions').remove('attraction0');
							excursionform.render('attractions');
						}
					}]
				}<?php } else {
					for ($i = 0; $i < $countAttractions; $i++) {
						?>{
					id: 'attraction<?php echo $i ?>',
					xtype:'fieldset',
					width: 500,
					labelWidth: 110,
					autoHeight:true,
					style: 'border: 0px; margin-bottom: 0px;',
					items: [{
						xtype:'textfield',
						fieldLabel: 'Название',
	    				name: 'Attraction[title][]',
	    				id: 'attractionTitle<?php echo $i ?>',
	    				width: 300,
	    				value: '<?php echo addcslashes($attractions[$i]['title'], "'\\") ?>'
					},{
						fieldLabel: 'Описание',
						xtype:'textarea',
	        			name: 'Attraction[description][]',
	        			id: 'attractionDescription<?php echo $i ?>',
	        			width: 300,
	        			value: document.getElementById('attractionTxt<?php echo $attractions[$i]['id'] ?>').value
					},{
						xtype: 'button',
						text: 'удалить',
						style: 'padding-left: 115px',
						handler: function() {
							excursionform.findById('attractions').remove('attraction<?php echo $i ?>');
							excursionform.render('attractions');
						}
					}]
				}<?php
						if ($i != ($countAttractions - 1)) {
							echo ',';
						}
					}
				}?>],
				buttons: [{
					text: 'Добавить достопримечательность',
					handler: function() {
						var field = new Ext.form.FieldSet({
							id: 'attraction' + attractionFieldCounter,
							width: 500,
							labelWidth: 110,
							autoHeight:true,
							style: 'border: 0px; margin-bottom: 0px;',
							items: [{
								xtype:'textfield',
								fieldLabel: 'Название',
			    				name: 'Attraction[title][]',
			    				id: 'attractionTitle' + attractionFieldCounter,
			    				width: 300
							},{
								fieldLabel: 'Описание',
								xtype:'textarea',
			        			name: 'Attraction[description][]',
			        			id: 'attractionDescription' + attractionFieldCounter,
			        			width: 300
							},{
								xtype: 'button',
								text: 'удалить',
								style: 'padding-left: 115px',
								numElem: attractionFieldCounter,
								handler: function() {
									excursionform.findById('attractions').remove('attraction' + parseInt(this.numElem));
									excursionform.render('attractions');
								}
							}]
						});
						
						attractionFieldCounter++;
						excursionform.findById('attractions').add(field);
						excursionform.render('attractions');
					}
				}]
        	}]
		},{
            xtype:'fieldset',
            id: 'setType<?php echo $excursionTypes[1][0] ?>',
            title: '<?php echo $excursionTypes[1][1] ?> экскурсия',
            autoHeight:true,
            defaults: {width: 210},
			labelWidth: 130,
            defaultType: 'textfield',
            hidden: <?php echo (($excursion->attributes['excursionTypeId'] != $excursionTypes[1][0]) ? 'true' : 'false') ?>,
            items :[{
                name: 'Excursion[route<?php echo $excursionTypes[1][0] ?>]',
                xtype: 'ckeditor',
                hideLabel: true,
                CKConfig: {
                    customConfig : '<?php echo Yii::app()->request->baseUrl; ?>/public/assets/ckeditor/config.js',
                    toolbar: 'Str',
                    height : 300,
                    width: 540
                },
				width: 420,
				height: 70,
				value: <?php echo (($excursion->attributes['excursionTypeId'] == $excursionTypes[1][0]) ? 'document.getElementById(\'excursionTxt\').value' : "''") ?>
			},{
				xtype: 'spinnerfield',
				fieldLabel: 'Цена (руб)',
				name: 'Excursion[price<?php echo $excursionTypes[1][0] ?>]',
				minText: 'Минимальное допустимое значение: {0}',
				minValue: 0,
				allowDecimals: true,
				decimalPrecision: 2,
				incrementValue: 1000,
				accelerate: true,
				value: <?php echo (($excursion->attributes['excursionTypeId'] == $excursionTypes[1][0]) ? $excursion->attributes['price'] : 0) ?>
        	},{
				xtype: 'spinnerfield',
				fieldLabel: 'Цена (шекель)',
				name: 'Excursion[price<?php echo $excursionTypes[1][0] ?>Shek]',
				minText: 'Минимальное допустимое значение: {0}',
				minValue: 0,
				allowDecimals: true,
				decimalPrecision: 2,
				incrementValue: 1000,
				accelerate: true,
				value: <?php echo (($excursion->attributes['excursionTypeId'] == $excursionTypes[1][0]) ? $excursion->attributes['price2'] : 0) ?>
        	}]
		},{
            xtype:'fieldset',
            id: 'setType<?php echo $excursionTypes[2][0] ?>',
            title: '<?php echo $excursionTypes[2][1] ?> экскурсия',
            autoHeight:true,
            defaults: {width: 540},
			hideLabel: true,
            defaultType: 'textfield',
            hidden: <?php echo (($excursion->attributes['excursionTypeId'] != $excursionTypes[2][0]) ? 'true' : 'false') ?>,
            items :[{
                name: 'Excursion[route<?php echo $excursionTypes[2][0] ?>]',
                xtype: 'ckeditor',
                hideLabel: true,
                CKConfig: {
                    /* Enter your CKEditor config paramaters here or define a custom CKEditor config file. */
                    customConfig : '<?php echo Yii::app()->request->baseUrl; ?>/public/assets/ckeditor/config.js',
                    toolbar: 'Str',
                    height : 300,
                    width: 540
                },
				width: 540,
				height: 70,
				value: <?php echo (($excursion->attributes['excursionTypeId'] == $excursionTypes[2][0]) ? 'document.getElementById(\'excursionTxt\').value' : "''") ?>
			}]
		}],
		

        buttons: [{
            text: 'Сохранить',
			handler: function(b) {
				var fp = this.ownerCt.ownerCt,
					form = fp.getForm();
				if (form.isValid()) {
					b.disable();
					form.getEl().dom.action = '/admin/excursion/edithandler/id/<?php echo $excursion->attributes['id'] ?>';
					form.submit();
            	} else {
					formhotelcreate.form.markInvalid();
					Ext.MessageBox.alert('Ошибка!', 'Ошибка, информация неверная! Проверьте правильность заполнения полей!');
				}
			}
        },{
            text: 'Отменить',
            handler: function() {
            	location.href = '/admin/excursion/';
            }
        }]
    });

    excursionform.render(Ext.get('excursiondiv'));
});

function changeFieldSet(combo, record, index)
{
	var fieldSetValue = parseInt(record.data.value);
	
	excursionform.findById('setType' + currentFieldSet).hide();
	excursionform.findById('setType' + fieldSetValue).show();
	currentFieldSet = fieldSetValue; 
}
</script>
<div id="excursiondiv"></div>